#include "playscene.h"
#include <QAction>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QPainter>
#include <QPropertyAnimation>
#include <QSound>
#include <QtDebug>
#include "mypushbutton.h"
#include <QTimer>
#include "dataconfig.h"
#include "mycoin.h"
PlayScene::PlayScene(int level)
{
    QString str =  QString ("进入了第%1关").arg(level);
    qDebug()<<str;
    this->levelIndex  =  level;//关卡

    //音效
    QSound * backSound = new QSound(":/res/BackButtonSound.wav",this);
//金币音效
    QSound * coinSound = new QSound(":/res/ConFlipSound.wav",this);
    //胜利音效
    QSound * winSound = new QSound(":/res/LevelWinSound.wav",this);

    //顺手设置一下初始化参数
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("翻金币场景");

    //创建菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    //创建开始菜单栏
    QMenu * startMenu = bar->addMenu("开始");
    //创建退出 菜单项
    QAction *quitAction = startMenu->addAction("退出");

    //点击退出 实现退出游戏
    connect(quitAction,&QAction::triggered,[=]() {
        this->close();
    });
    //返回按钮
    myPushButton * backBtn = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    //定位右下角
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    connect(backBtn,&myPushButton::clicked,[=]() {
        backSound->play();
        //告诉主场景返回信号,主场景监听ChooseLevelScene的返回按钮
        qDebug()<<"金币场景点击了返回按钮";
        QTimer::singleShot(200,this,[=]() {
            emit this->chooseSceneBack();
        });
    });
    //显示当前关卡数
    QLabel *label = new QLabel;
    label->setParent(this);
    QFont font;
    //设置字体
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString levelStr = QString("Level:%1").arg(this->levelIndex);
    label->setText(levelStr);
    //参数1开始x位置,参数2开始y位置,画矩形参数可以有多个
    label->setGeometry(30,this->height()-50,120,50);

//声明游戏数组
    dataConfig config;
    //初始化每关的二维数组
    for (int i=0; i<4; i++) {
        for(int j = 0 ; j<4; j++) {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];

        }
    };
    //创建胜利图片显示的样式
    QLabel *winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    //图片隐藏在屏幕外面  下面使用线性动画进行一个显示
    winLabel->move(((this->width()-tmpPix.width())*0.5),-tmpPix.height());



//根据数组显示金币或者银币
    //创建金币的背景图片
    for (int i=0; i<4; i++) {
        for(int j = 0 ; j<4; j++) {

            QString coinStr;
            if(this->gameArray[i][j]==1) {
//显示金币
                str=":/res/Coin0001.png";
            } else {
                str=":/res/Coin0008.png" ;
            }
            //绘制背景图片
            QPixmap pix = QPixmap(":/res/BoardNode(1).png");
            QLabel* label = new QLabel   ;
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix );
            label->setParent(this);
            label->move(57+i*50,200+j*50);

            //创建金币
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            //位置微调剧中
            coin->move(59+i*50,204+j*50);
            //金币赋值
            coin->posX = i; //格子数 ,计算坐标 乘以系数就可以了
            coin->posY = j;
            coin->flag  =  this->gameArray[i][j];//正反标志
            //将金币放入到 二维数组中维护
            btnCoin[i][j] = coin;
            //金币点击, 反转
            connect(coin,&MyCoin::clicked,[=]() {
                coinSound->play();
                coin->checkPoint = true;
                coin->changeFlag();
                //记得更新数组的信息
                this->gameArray[i][j] = this->gameArray[i][j]==0?1:0;
                //周围环境反转
                //来个延时翻转效果
                QTimer::singleShot(200,this,[=]() {
                    if(coin->posX+1<=3)//周围右侧
                    {
                        btnCoin[coin->posX+1][coin->posY]->changeFlag();
                        //状态更新
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                    }
                    //左侧硬币反转条件
                    if(coin->posX-1>=0)//周围左侧
                    {
                        btnCoin[coin->posX-1][coin->posY]->changeFlag();
                        //状态更新
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                    }
                    //  上侧金币反转判断
                    if(coin->posY+1<=3)//周围上侧
                    {
                        btnCoin[coin->posX][coin->posY+1]->changeFlag();
                        //状态更新
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                    }

                    //下侧金币反转判断
                    if(coin->posY-1>=0)//周围下侧
                    {
                        btnCoin[coin->posX][coin->posY-1]->changeFlag();
                        //状态更新
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                    }
                    coin->checkPoint = false;
                    this->isWin=true;
                    //判断是否胜利
                    for(int i = 0 ; i <4 ; i++) {
                        for(int j =0 ; j<4 ; j++) {
                            //如果有任意一个硬币的面状态为反面则失败
                            if(btnCoin[i][j]->flag == false) {
                                this->isWin =false;
                                break;
                            }
                        }
                    }
                    if(this->isWin == true) {
                        //胜利了
                        qDebug()<<"胜利了";
                        //更改按钮胜利状态 用于禁用按键, 写法不唯一,此种方式并不高效
                        for(int i = 0 ; i <4 ; i++) {
                            for(int j =0 ; j<4 ; j++) {

                                //将所有的按钮的胜利标记改为true
                                btnCoin[i][j]->isWin = true ;
                            }
                        }
                        //胜利图片显示  向下移动
                        //线性动画
                        //创建动态对象
                        QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
                        //设置动画事件间隔
                        animation->setDuration(1000);
                        //起始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //结束位置  位置靠下,模拟按下效果
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                        //设置弹跳曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        //胜利音效
                        winSound->setLoops(-1);
                        winSound->play();
                        //循环播放, 如果是-1 无限循环

                        //执行动画
                        animation->start();

                    }



                });

            });

        }
    }
    // 矩阵中的坐标需要使用,所以不通过一次获取


}
//背景画面
void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap  pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
    pix.load(":/res/Title.png");
    //这里调整的是正好容得下pix的起点
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);

    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);


}
